package com.sczmgk.hotline_pzh.base;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.Message;
import android.support.annotation.NonNull;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.core.BitmapSize;
import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.callback.HttpFailCallback;
import com.sczmgk.hotline_pzh.callback.SimplePaserJsonCallback;
import com.sczmgk.hotline_pzh.constants.Constants;
import com.sczmgk.hotline_pzh.dialog.DialogFactory;
import com.sczmgk.hotline_pzh.entity.FileBean;
import com.sczmgk.hotline_pzh.entity.MediaBean;
import com.sczmgk.hotline_pzh.httputils.MyHttp;
import com.sczmgk.hotline_pzh.photo.PhotoActivity;
import com.sczmgk.hotline_pzh.photo.PhotoAlbumActivity;
import com.sczmgk.hotline_pzh.photo.entity.PhotoItem;
import com.sczmgk.hotline_pzh.popup.PupDialog;
import com.sczmgk.hotline_pzh.utils.CameraUtil;
import com.sczmgk.hotline_pzh.utils.CommonUtils;
import com.sczmgk.hotline_pzh.utils.ContentUtil;
import com.sczmgk.hotline_pzh.utils.FileUtil;
import com.sczmgk.hotline_pzh.utils.MyLog;
import com.sczmgk.hotline_pzh.utils.MyToast;
import com.sczmgk.hotline_pzh.utils.Tools;
import com.sczmgk.hotline_pzh.view.MyGridView;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;


/**
 * Created by Administrator on 2016/12/2 0002.
 */
public abstract class UploadPicBaseActivity extends BaseActivity implements View.OnClickListener{

    private static final int REFRESH_LIST = 101;

    /**
     * 添加音频视频和图片的控件
     */
    private ImageView mAddAudio;
    private ImageView mAddVideo;
    protected ImageView mAddpic;

    /**
     * 音频和视频的名字
     */
    private TextView mVideo;
    private TextView mAudio;

    /**
     * 音频和视频的JavaBean
     */
    protected MediaBean mediaAudio;
    protected MediaBean mediaVideo;

    /**
     * 删除音频和视频的控件
     */
    private ImageView mDeleteAudio;
    private ImageView mDeleteVideo;

    /**
     * 视频和音频缩略图
     */
    private ImageView mVideoPic;
    private ImageView mAudioPic;

    /**
     * 音频和视频的容器
     */
    private LinearLayout mVideoContainer;
    private LinearLayout mAudioContainer;

    /**
     * 添加图片的控件
     */
    private MyGridView myGridView;

    /**
     * 图片的集合
     */
    private LinkedList<PhotoItem> displayPics = new LinkedList<>();

    /**
     * 添加图片的Adapter
     */
    private MyAdapter adapter;

    /**
     * 上传音频的url
     */
    protected String audioUrl ;

    /**
     * 上传视频的url
     */
    protected String videoUrl ;

    private Uri mNewPhotoUri;
    protected BitmapUtils bitmapUtils;

    /**
     * 原子boolean类型,用于标记上传队列是否完成
     */
    public static final AtomicBoolean isUploading = new AtomicBoolean();

    /**
     * 已上传到服务器的图片URL
     */
    protected List<String> uploadfileUrls = new ArrayList<>();

    /**
     * 初始化控件
     */
    public void initView() {

        //添加音频,视频,图片的控件
        mAddAudio = (ImageView) findViewById(R.id.iv_add_voice);
        mAddVideo = (ImageView) findViewById(R.id.iv_add_video);
        mAddpic = (ImageView) findViewById(R.id.iv_add_pic);

        //删除音频视频的控件
        mDeleteAudio = (ImageView) findViewById(R.id.iv_upload_delete_voice);
        mDeleteVideo = (ImageView) findViewById(R.id.iv_upload_delete_video);

        //音频视频的名字
        mAudio = (TextView) findViewById(R.id.tv_upload_voice_name);
        mVideo = (TextView) findViewById(R.id.tv_upload_video_name);

        //视频音频的缩略图
        mVideoPic = (ImageView) findViewById(R.id.iv_video_pic);
        mAudioPic = (ImageView) findViewById(R.id.iv_voice_pic);

        //视频音频的容器
        mVideoContainer = (LinearLayout)findViewById(R.id.ll_video_container);
        mAudioContainer = (LinearLayout)findViewById(R.id.ll_audio_container) ;

        //设置监听
        loadingDialog = DialogFactory.getLoadingDialog(this);
        setListener();
        bitmapUtils = new BitmapUtils(this);
        bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.ARGB_4444);
        bitmapUtils.configDefaultLoadFailedImage(getResources().getDrawable(R.drawable.default_center));
        bitmapUtils.configDefaultLoadingImage(getResources().getDrawable(R.drawable.ic_logo));
//        displayConfig = new BitmapDisplayConfig();
//        BitmapSize bitmapSize = new BitmapSize(50, 50);
//        displayConfig.setBitmapMaxSize(bitmapSize);
        bitmapUtils.configDefaultBitmapMaxSize(100, 100);

//        PhotoItem picBean = new PhotoItem();
//        displayPics.add(picBean);

        //添加图片的控件
        myGridView = (MyGridView) findViewById(R.id.gv_complain_pic);
        adapter = new MyAdapter();
        myGridView.setAdapter(adapter);

        isUploading.set(false);
    }


    /**
     * 取得拼装好的图片URL   用"/"分开
     *
     * @return
     */
    public String getUploadFileUrls() {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < uploadfileUrls.size(); i++) {
            if (i != 0) {
                sb.append("/");
            }
            sb.append(uploadfileUrls.get(i));
        }
        //TODO 拼装音频视频

        return sb.toString();
    }

    /**
     * 设置监听
     */
    private void setListener() {

        mAddpic.setOnClickListener(this);
        mAddAudio.setOnClickListener(this);
        mAddVideo.setOnClickListener(this);
        mDeleteVideo.setOnClickListener(this);
        mDeleteAudio.setOnClickListener(this);
        if (loadingDialog != null) {
            loadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
                @Override
                public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                    finish();
                    return false;
                }
            });
        }
    }


    /**
     * 功能 Adapter
     *
     * @author Administrator
     */
    public class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return displayPics.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {

            View view = View.inflate(UploadPicBaseActivity.this, R.layout.complain_pic_item, null);

            ImageView addPic = (ImageView) view.findViewById(R.id.iv_complain_pic);

            ImageButton deletePic = (ImageButton) view.findViewById(R.id.ib_delete_pic);



            deletePic.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    MyLog.e(TAG, "点击了删除图片-------");
                    displayPics.remove(displayPics.get(position));
                    Message msg = Message.obtain();
                    msg.what = REFRESH_LIST;
                    mHandler.sendMessage(msg);
                }
            });

            if (Tools.isEmpty(displayPics.get(position).getPhotoPath())) {
                MyLog.d(TAG, "这是加图片的按钮。。。。。");
                addPic.setBackgroundResource(R.drawable.ic_add_pic);
                deletePic.setVisibility(View.GONE);
            } else {
                bitmapUtils.display(addPic, displayPics.get(position).getPhotoPath().toString());
            }
            return view;
        }

    }

    /**
     * 选择图片pop的点击事件
     *
     * @author
     */
    class PhotoPupEvent implements PupDialog.PupEvent {

        @Override
        public void itemOnClick(PupDialog pupid, View item, int position) {
            switch (position) {
                case 0:
                    takePhoto();
                    break;
                case 1:
                    //去相册
                    Intent intent = new Intent(UploadPicBaseActivity.this, PhotoAlbumActivity.class);
                    List<PhotoItem> selectLists = new ArrayList<>();
                    //准备已先择数据
                    for (PhotoItem photoItem : displayPics) {
                        if (!Tools.isEmpty(photoItem.getPhotoPath())) {
                            selectLists.add(photoItem);
                        }
                    }
                    //传入已选择的图片
                    intent.putExtra(Constants.SELECTED_PIC_LIST, (Serializable) selectLists);

                    startActivityForResult(intent, Param.REQUEST_CODE_SELECT_PHOTO);
                    break;

                default:
                    break;

            }
            pupid.dismiss();
        }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        MyLog.d(TAG, "requestCode: " + requestCode + "      resultCode: " + resultCode);

        if (resultCode == RESULT_OK) {

            Uri uri = null ;
            if (intent != null) {
                uri = intent.getData();
            }
            switch (requestCode) {
                case Param.PHOTOTAKEN_REQUEST_SIGN: // 拍照
                    MyLog.i(TAG, "拍照返回了.....");
                    displayPics.addFirst(new PhotoItem((mNewPhotoUri.toString().substring(7))));
                    Message msg1 = Message.obtain();
                    msg1.what = REFRESH_LIST;
                    mHandler.sendMessage(msg1);
                    break;
                case Param.TAKE_PICTURE_SIGN: // 选择相册单张照片返回
                    if (intent == null) {
                        return;
                    }
                    //先判断图片是否存在

                    MyLog.e(TAG, "取得相册回调uri ：" + uri.toString());
                    MediaBean path = ContentUtil.getPath(UploadPicBaseActivity.this,uri);
                    if (!Tools.isEmpty(path.getPath())) {
                        MyLog.e(TAG, "转换成绝对路径 ：" + path);

                        boolean imageIsExist = imageIsExist(path.getPath());
                        if (!imageIsExist) {
                            if (displayPics.size() >= PhotoActivity.CHOOSE_MAX_COUNT) {
                                MyToast.show(UploadPicBaseActivity.this, String.format(getString(R.string.photo_max_choose), PhotoActivity.CHOOSE_MAX_COUNT + ""));
                            } else {
                                displayPics.addFirst(new PhotoItem(path.getPath()));
                            }
                        }
                        Message msg2 = Message.obtain();
                        msg2.what = REFRESH_LIST;
                        mHandler.sendMessage(msg2);
                    } else {
                        MyToast.show(UploadPicBaseActivity.this, "选择图片失败！");
                    }
                    break;
                case Param.REQUEST_CODE_SELECT_PHOTO:


                    List<PhotoItem> newPhotoList = (List<PhotoItem>) intent.getSerializableExtra(PhotoActivity.ResultParam.SELECT_PHOTO_LIST);


                    displayPics.clear();

                    for (PhotoItem photoItem : newPhotoList) {
                        displayPics.addFirst(photoItem);
                    }
                    Message msg2 = Message.obtain();
                    msg2.what = REFRESH_LIST;
                    mHandler.sendMessage(msg2);
                    break;
                case Param.REQUEST_CODE_SELECT_AUDIO:
                    mediaAudio = ContentUtil.getPath(UploadPicBaseActivity.this, uri);

                    if (mediaAudio == null){
                        return ;
                    }

                    long audioSize = mediaAudio.getSize();
                    double sizeAudio = formetFileSize(audioSize);
                    MyLog.d(TAG , String.valueOf(sizeAudio));
                    if (sizeAudio > 5){
                        MyToast.show(UploadPicBaseActivity.this ,"文件大小不能大于5M");
                        return;
                    }

                    if(mediaAudio != null){
                        setAddButtonClickable(mAudioContainer,mAddAudio,false,mAudioPic,mediaAudio);
                        mAudio.setText(mediaAudio.getTitle());
                    }

                    break;
                case Param.REQUEST_CODE_SELECT_VIDEO:
                    mediaVideo = ContentUtil.getPath(UploadPicBaseActivity.this, uri);

                    if (mediaVideo == null){
                        return ;
                    }
                    long videoSize = mediaVideo.getSize();
                    double sizeVideo = formetFileSize(videoSize);
                    MyLog.d(TAG , String.valueOf(sizeVideo));
                    if (sizeVideo > 20){
                        MyToast.show(UploadPicBaseActivity.this ,"文件大小不能大于20M");
                        return;
                    }

                    if(mediaVideo != null){
                        setAddButtonClickable(mVideoContainer,mAddVideo,false,mVideoPic,mediaVideo);
                        mVideo.setText(mediaVideo.getTitle());

                    }
                    break;
                default:
                    break;
            }
        }
    }

    public double formetFileSize(long fileSize) {
//        double mbsize = fileSize / 1024f / 1024f;
//        DecimalFormat df = new DecimalFormat("#.00");
//        DecimalFormat df=new DecimalFormat(".##");
//        String format12 = df.format(fileSize / 1024f / 1024f);
//        double fileSizeFormat = new Double(Math.round(fileSize/1024f/1024f)/1000.0);

        BigDecimal result = new BigDecimal(String.valueOf(fileSize)).divide(new BigDecimal(String.valueOf(1048576)), 2, BigDecimal.ROUND_HALF_UP);
        String format = result.toString();
        Double fileSizeFormat = Double.valueOf(result.toString());
        return fileSizeFormat;
    }

    /**
     * 设置添加按钮的背景以及是否可点击
     * 视频音频的容器的显示
     * @param container 容器
     * @param addButton 添加按钮的控件
     * @param isEnable 是否允许被点击
     * @param pic 缩略图控件
     * @param mediaBean JavaBean
     */
    private void setAddButtonClickable(LinearLayout container,ImageView addButton,boolean isEnable,ImageView pic,MediaBean mediaBean){

        if(isEnable){
            container.setVisibility(View.GONE);
            pic.setVisibility(View.GONE);
            addButton.setBackgroundResource(R.drawable.add_file);
            addButton.setClickable(true);
        }else{

            if(mediaBean != null){

                //获取缩略图
                Bitmap bitmap = getVideoThumbnail(mediaBean.getPath());
                if(bitmap != null){
                    pic.setVisibility(View.GONE);
                    pic.setImageBitmap(bitmap);
                }
            }
            container.setVisibility(View.VISIBLE);
            addButton.setBackgroundResource(R.drawable.add_file_finish);
            addButton.setClickable(false);
        }
    }
//    /**
//     * 过时的根据URi取得文件的绝对路径
//     *
//     * @param uri
//     * @return
//     */
//    private String getAbsoluteImagePath(Uri uri) {
//        // can post image
//        String[] proj = {MediaStore.Images.Media.DATA};
//
//        Cursor cursor = managedQuery(uri,
//                proj,       // Which columns to return
//                null,       // WHERE clause; which rows to return (all rows)
//                null,       // WHERE clause selection arguments (none)
//                null);                 // Order-by clause (ascending by name)
//
//        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
//        cursor.moveToFirst();
//
//        return cursor.getString(column_index);
//    }

    private boolean imageIsExist(String uri) {
        boolean isExist = false;
        for (int i = 0; i < displayPics.size(); i++) {
            if (!Tools.isEmpty(displayPics.get(i).getPhotoPath()) && displayPics.get(i).getPhotoPath().equals(uri)) {
                isExist = true;
            }
        }
        return isExist;
    }

    /***
     * 拍照
     */
    private void takePhoto() {
        mNewPhotoUri = CameraUtil.getOutputMediaFileUri(this, CameraUtil.MEDIA_TYPE_IMAGE);

        CameraUtil.captureImage(this, mNewPhotoUri, Param.PHOTOTAKEN_REQUEST_SIGN);
    }

    public static class Param {
        /**
         * intent 传递联系人是否上传图像的key
         */
        public static final String INTENT_KEY_ISLoadLOGINUSERPHOTO = "loadloginuserphonto";
        public static final String INTENT_KEY_UPDAEINFO = "updateinfo";
        ;
        public static final int RESULT_CODE_UPDATE_NICKNAME = 0;
        public static final int RESULT_CODE_UPDATE_AREA = 2;
        public static final int PHOTOTAKEN_REQUEST_SIGN = 1010;

        /**
         * 系统相册返回
         */
        public static final int TAKE_PICTURE_SIGN = 1030;
        /**
         * 自定义相册返回
         */
        public static final int REQUEST_CODE_SELECT_PHOTO = 1040;
        /**
         * 图像裁切后的回调
         */
        public static final int REQUEST_PHOTO_RESULT = 204;
        /**
         * 选择音频返回
         */
        public static final int REQUEST_CODE_SELECT_AUDIO = 1050;
        /**
         * 选择视频返回
         */
        public static final int REQUEST_CODE_SELECT_VIDEO = 1060;
    }

    @Override
    public void handleMessage(Message msg) {

        switch (msg.what) {
            case REFRESH_LIST:
                adapter.notifyDataSetChanged();
                if(displayPics.size()==PhotoActivity.CHOOSE_MAX_COUNT){
                    mAddpic.setBackgroundResource(R.drawable.add_file_finish);
                    mAddpic.setClickable(false);
                }else{
                    mAddpic.setBackgroundResource(R.drawable.add_file);
                    mAddpic.setClickable(true);
                }
                break;
        }
    }
    public void uploadFile(final OnUploadCallback onUploadCallback){
        //1.上传图片
        uploadPic(onUploadCallback, Constants.PIC_MODULE_COMPLAIN);
    }

    /**
     * 上传图片
     *
     * @param onUploadCallback 上传完成的回调
     */
    public void uploadPic(final OnUploadCallback onUploadCallback,final int module) {
        DialogFactory.showDialog(loadingDialog);
        BitmapUtils bm = new BitmapUtils(UploadPicBaseActivity.this);
        bm.configDefaultBitmapMaxSize(new BitmapSize(800, 800));
        if (displayPics.size() > 0) {
            //1.先判断图片路径是否为空
            isUploading.set(true);
            PhotoItem first = displayPics.getFirst();
            if (!Tools.isEmpty(first.getPhotoPath())) {
                PhotoItem firstData = displayPics.getFirst();
                String imagePath = firstData.getPhotoPath();
                MyLog.i(TAG, "有图片开始上传。。。。 ： " + imagePath);
                final Map<String, Object> uploadMaps = getStringObjectMap(module);
                String uploadPicTempFilePath = FileUtil.getUploadPicTempFilePath();
                MyLog.d(TAG, "原图片路径 :　 " + imagePath + "临时文件路径　：　" + uploadPicTempFilePath);
                //1.将图片压缩存入临时文件
                com.sczmgk.hotline_pzh.utils.BitmapUtils.compressBitmap(uploadPicTempFilePath, imagePath, 800, 800, new com.sczmgk.hotline_pzh.utils.BitmapUtils.CompressBitmapCallback() {
                    @Override
                    public void compress(final String imagePath) {
                        MyLog.d(TAG, "另存压缩临时图片的路径是：" + imagePath);

                        MyHttp.requestByUploadFile(Constants.BASE_URL + Constants.POST_UPLOD_FILE_URL, imagePath, uploadMaps, MyHttp.UPLOAD_FILE, UploadPicBaseActivity.this,
                                false, null, new SimplePaserJsonCallback() {//这时加上dilog会有异常

                                    @Override
                                    public void onVerifySuccess(String dataRaw, boolean isCache) {
                                        displayPics.removeFirst();
                                        boolean deleteDirectory = FileUtil.delete(imagePath);
                                        if (deleteDirectory) {
                                            MyLog.d(TAG, "删除临时图片成功 -------------" + imagePath);
                                        } else {
                                            MyLog.e(TAG, "删除临时图片失败----------路径是：" + imagePath);
                                        }
                                        paserDataJson(dataRaw, onUploadCallback,module);//上传成功

                                    }

                                    @Override
                                    public void onVerifyIsNull() {

                                    }

                                }, new HttpFailCallback() {

                                    @Override
                                    public void onServiceErrorCallback() {
                                        isUploading.set(false);
                                        DialogFactory.dismissLoadingDialog(loadingDialog);

                                    }
                                });

                    }
                });
            }

        } else {
            //2.上传音频
//            DialogFactory.dismissLoadingDialog(loadingDialog);
            uploadAudio(onUploadCallback, Constants.AUDIO_MODULE_COMPLAIN);
        }
    }

    private void uploadAudio(final OnUploadCallback onUploadCallback,final int module) {
        if(mediaAudio != null){
            isUploading.set(true);
            //上传音频
//            DialogFactory.showDialog(loadingDialog);
            final Map<String, Object> uploadMaps = getStringObjectMap(module);
            MyHttp.requestByUploadFile(Constants.BASE_URL + Constants.POST_UPLOD_FILE_URL, mediaAudio.getPath(), uploadMaps, MyHttp.UPLOAD_FILE, UploadPicBaseActivity.this,
                    false, null, new SimplePaserJsonCallback() {//这时加上dilog会有异常

                        @Override
                        public void onVerifySuccess(String dataRaw, boolean isCache) {
                            mediaAudio=null;//TODO 要处理
                            paserDataJson(dataRaw, onUploadCallback,module);//上传成功

                        }

                        @Override
                        public void onVerifyIsNull() {

                        }

                    }, new HttpFailCallback() {

                        @Override
                        public void onServiceErrorCallback() {
                            isUploading.set(false);
                            DialogFactory.dismissLoadingDialog(loadingDialog);

                        }
                    });

        }else{
//            DialogFactory.dismissLoadingDialog(loadingDialog);
            //3.上传视频
            uploadVideo(onUploadCallback, Constants.VIDEO_MODULE_COMPLAIN);
        }
    }
    private void uploadVideo(final OnUploadCallback onUploadCallback,final int module) {
        if(mediaVideo != null){
            isUploading.set(true);
            //上传音频
//            DialogFactory.showDialog(loadingDialog);
            final Map<String, Object> uploadMaps = getStringObjectMap(module);
            MyHttp.requestByUploadFile(Constants.BASE_URL + Constants.POST_UPLOD_FILE_URL, mediaVideo.getPath(), uploadMaps, MyHttp.UPLOAD_FILE, UploadPicBaseActivity.this,
                    false, null, new SimplePaserJsonCallback() {//这时加上dilog会有异常

                        @Override
                        public void onVerifySuccess(String dataRaw, boolean isCache) {
                            mediaVideo=null;
                            paserDataJson(dataRaw, onUploadCallback,module);//上传成功

                        }

                        @Override
                        public void onVerifyIsNull() {

                        }

                    }, new HttpFailCallback() {

                        @Override
                        public void onServiceErrorCallback() {
                            isUploading.set(false);
                            DialogFactory.dismissLoadingDialog(loadingDialog);

                        }
                    });
        }else{
//            DialogFactory.dismissLoadingDialog(loadingDialog);
            if(onUploadCallback != null){
                onUploadCallback.onUploadSuccess();
            }
        }
    }

    @NonNull
    private Map<String, Object> getStringObjectMap(int module) {
        final Map<String, Object> uploadMaps = new HashMap<String, Object>();

        String desc = "desc";
        String relateid = "1";
        String remark = "remark";
        //start
        String size = "100*100";
        int time = 100;
        //TODO 时长图片大小信息
        // end
        uploadMaps.put("desc", desc);
        uploadMaps.put("size", size);
        uploadMaps.put("relateid", relateid);
        uploadMaps.put("module", module);
        uploadMaps.put("time", time);
        uploadMaps.put("remark", remark);
        return uploadMaps;
    }

    /**
     * @param dataRaw
     */
    private void paserDataJson(String dataRaw, OnUploadCallback onUploadCallback,final int module) {
        if (Tools.isEmpty(dataRaw)) {
            MyToast.showDebug(UploadPicBaseActivity.this, "json  error!");
            return;
        }
        MyLog.d(TAG, "-取得存取图片返回的 JSON :    " + dataRaw);
        Gson gson = new Gson();
        FileBean fileBean = gson.fromJson(dataRaw, FileBean.class);
        List<FileBean.FileName> datalist = fileBean.datalist;
        String picPath = "";
        for (int i = 0; i < datalist.size(); i++) {
            picPath = datalist.get(i).filename;
        }
        //判断模块
        switch(module){
            case Constants.PIC_MODULE_COMPLAIN:
                uploadfileUrls.add(picPath);//加入到集合
                Message msg = Message.obtain();
                msg.what = REFRESH_LIST;
                mHandler.sendMessage(msg);
                uploadPic(onUploadCallback,module);
                break;
            case Constants. AUDIO_MODULE_COMPLAIN:
                audioUrl = picPath ;
                uploadAudio(onUploadCallback,module);

                break;
            case Constants.VIDEO_MODULE_COMPLAIN:
                videoUrl = picPath ;
                uploadVideo(onUploadCallback,module);
                break;

        }


    }


    /**
     * 上传成功的回调接口
     */
    public interface OnUploadCallback {
        /**
         * 全部图片上传时回调
         */
        void onUploadSuccess();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_add_pic:

                CommonUtils.hideSoftKeyboard(UploadPicBaseActivity.this);
                MyLog.d(TAG, "点击了添加图片-------");
                if (displayPics.size() <= PhotoActivity.CHOOSE_MAX_COUNT) {
                    showPup(PupDialog.ID_PHOTO, new PhotoPupEvent());
                } else {
                    MyToast.show(UploadPicBaseActivity.this, String.format(getString(R.string.photo_max_choose), PhotoActivity.CHOOSE_MAX_COUNT + ""));
                }
                break;
            case R.id.iv_add_voice:
                Intent intent=new Intent(Intent.ACTION_GET_CONTENT);
                intent.setType("audio/*");
                Intent chooser=Intent.createChooser(intent,"选择音频");
                startActivityForResult(chooser, Param.REQUEST_CODE_SELECT_AUDIO);
                break;
            case R.id.iv_add_video:
                Intent intent1=new Intent(Intent.ACTION_GET_CONTENT);
                intent1.setType("video/*");
                Intent chooser1=Intent.createChooser(intent1,"选择视频");
                startActivityForResult(chooser1, Param.REQUEST_CODE_SELECT_VIDEO);
                break;
            case R.id.iv_upload_delete_voice:
                setAddButtonClickable(mAudioContainer,mAddAudio,true,mAudioPic,null);
                mediaAudio = null;
                break;
            case R.id.iv_upload_delete_video:
                setAddButtonClickable(mVideoContainer,mAddVideo,true,mVideoPic,null);
                mediaVideo = null;
                break;
        }


    }

    /**
     * 获取缩略图
     * @param filePath 文件路径
     * @return Bitmap
     */
    public Bitmap getVideoThumbnail(String filePath) {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            retriever.setDataSource(filePath);
            bitmap = retriever.getFrameAtTime();
        }
        catch(IllegalArgumentException e) {
            e.printStackTrace();
        }
        catch (RuntimeException e) {
            e.printStackTrace();
        }
        finally {
            try {
                retriever.release();
            }
            catch (RuntimeException e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }
}
